package com.eastmoney.emis.edda.infrastructure.util;

import org.pgpainless.sop.SOPImpl;
import sop.SOP;

import java.io.IOException;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.util
 * @author: 汪程程
 * @date: 2022/9/26 2:55 PM
 * @version: 1.0
 */
public class OpenPGPUtils {
    private static final SOP sop = new SOPImpl();

    public static byte[] signAndEncrypt(byte[] data, byte[] publicKey, byte[] signKey, String keyPassword) throws IOException {
        return sop.encrypt()
                .signWith(signKey)
                .withKeyPassword(keyPassword)
                .withCert(publicKey)
                .plaintext(data)
                .getBytes();
    }

    public static byte[] encrypt(byte[] data, byte[] cert) throws IOException {
        return sop.encrypt()
                .withCert(cert)
                .plaintext(data)
                .getBytes();
    }

    public static byte[] decrypt(byte[] data, byte[] privateKey, String keyPassword) throws IOException {
        return sop.decrypt()
                .withKey(privateKey)
                .withKeyPassword(keyPassword)
                .ciphertext(data)
                .toByteArrayAndResult()
                .getBytes();
    }

    public static byte[] verifyAndDecrypt(byte[] data, byte[] cert, byte[] privateKey, String keyPassword) throws IOException {
        return sop.decrypt()
                .verifyWithCert(cert)
                .withKey(privateKey)
                .withKeyPassword(keyPassword)
                .ciphertext(data)
                .toByteArrayAndResult().getBytes();
    }

}
